package com.rjpacket.supernews.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import com.rjpacket.supernews.app.models.User;

import java.util.HashMap;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, PlatformActionListener, Handler.Callback {


    private TextView login_wechat;
    private TextView login_sina;
    private TextView login_tweibo;
    private SharedPreferences share;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        share = getSharedPreferences(Constants.SP_APP, MODE_PRIVATE);

        login_wechat = (TextView) findViewById(R.id.activity_login_weichat);
        login_sina = (TextView) findViewById(R.id.activity_login_sina);
        login_tweibo = (TextView) findViewById(R.id.activity_login_qqweibo);

        login_sina.setOnClickListener(this);
        login_wechat.setOnClickListener(this);
        login_tweibo.setOnClickListener(this);

        ImageView yunDuo = (ImageView) findViewById(R.id.yunduo_move);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_to_right);

        yunDuo.setAnimation(animation);

        animation.start();

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.activity_login_sina:
                ShareSDK.initSDK(this);
                Platform weibo = ShareSDK.getPlatform(this, SinaWeibo.NAME);
                weibo.setPlatformActionListener(this);
                weibo.authorize();

                weibo.SSOSetting(true);
//移除授权
//weibo.removeAccount();
                break;
            case R.id.activity_login_weichat:
                break;
            case R.id.activity_login_qqweibo:
                break;
            default:
                //点击回退
                finish();
                break;
        }

        super.onClick(v);
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        // 分享时Notification的图标和文字
        oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        oks.setText("我是分享文本");
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg");
        // 启动分享GUI
        oks.show(this);
    }

    private void authorize(Platform plat) {

        if (plat == null) {
            plat = new SinaWeibo(this);


        }
//判断指定平台是否已经完成授权
        if (plat.isValid()) {
            String userId = plat.getDb().getUserId();
            if (userId != null) {
                UIHandler.sendEmptyMessage(1, this);
                Log.d(plat.getName(), userId, null);
                return;
            }
        }
        plat.setPlatformActionListener(this);
        // true不使用SSO授权，false使用SSO授权
        plat.SSOSetting(true);
        //获取用户资料
        plat.showUser(null);
    }


    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        //注册成功回调
//        Toast.makeText(this,"成功授权",Toast.LENGTH_SHORT).show();
        if (platform.isValid() && platform instanceof SinaWeibo) {
            String userId = platform.getDb().getUserId();
            if (userId != null) {
                String userName = platform.getDb().getUserName();
                String userIcon = platform.getDb().getUserIcon();

                SharedPreferences.Editor editor = share.edit();
                editor.putBoolean("isLogin", true);
                editor.putString("userId", userId);
                editor.commit();

//                User user = new User();
//                user.setUserId(userId);
//                user.setUserName(userName);
//                user.setUserIcon(userIcon);
//                Message msg = new Message();
//                msg.obj = user;
//                msg.what = 1;
//                UIHandler.sendMessage(msg,this);

//                Intent intent = new Intent(this,UserInfoActivity.class);
//                startActivity(intent);
                finish();
                Toast.makeText(this, "user:" + user.getUserName(), Toast.LENGTH_SHORT).show();

            }
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        //注册失败
//        Toast.makeText(this,"授权失败，请重试。",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel(Platform platform, int i) {

    }

    @Override
    public boolean handleMessage(Message msg) {
        int what = msg.what;
        switch (what) {
            case 1:
                //sina
                Object obj = msg.obj;
                if (obj instanceof User) {
                    user = (User) obj;
                    Intent intent = new Intent(this, UserInfoActivity.class);
                    startActivity(intent);
                    finish();
                    Toast.makeText(this, "user:" + user.getUserName(), Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                //weixin
                break;
            case 3:
                //tencent
                break;
        }
        return false;
    }
}
